package com.wgy.designmode.action.strategy;

import com.wgy.designmode.action.strategy.context.Context;
import com.wgy.designmode.action.strategy.strategy.OperationAdd;
import com.wgy.designmode.action.strategy.strategy.OperationMultiply;
import com.wgy.designmode.action.strategy.strategy.OperationSubtract;
import com.wgy.designmode.action.strategy.strategy.Strategy;

/**
 * 策略模式
 * 示例：  对了两个数字做运算： 加、减、乘、除
 * 场景：解决在多种相似算法存在时，使用条件语句（如if...else）导致的复杂性和难以维护的问题。
 */
public class StrategyPatternDemo {

    public static void main(String[] args) {
        Strategy operationAdd = new OperationAdd();
        Strategy operationSubtract = new OperationSubtract();
        Strategy operationMultiply = new OperationMultiply();

        Context context = new Context();

        context.setStrategy(operationAdd);
        System.out.println("执行加法策略 结果：" + context.executeStrategy(2, 2));

        context.setStrategy(operationSubtract);
        System.out.println("执行减法策略 结果：" + context.executeStrategy(2, 2));

        context.setStrategy(operationMultiply);
        System.out.println("执行乘法策略 结果：" + context.executeStrategy(2, 2));

    }
}
